//
//  UserInfoManager.m
//  KEREN
//
//  Created by MW on 2023/3/24.
//

#import "UserInfoManager.h"

@implementation UserInfoManager
+(instancetype)sharedInstance{
    static UserInfoManager *manager = nil;
    if (manager == nil) {
        manager = [[UserInfoManager alloc] init];
    }
    return manager;
}

- (UserInfo *)getUserInfo{
    NSDictionary *dict =  [[NSUserDefaults standardUserDefaults] valueForKey:USERINFOKEY];
    UserInfo *userInfo = [UserInfo initWithDictionary:dict];
    //[[NSUserDefaults standardUserDefaults] setObject:userInfo.phone forKey:USERPHONE];
    return userInfo;
}
- (void)delectUserInfo{
    [[UserInfoManager sharedInstance].deviceInfoList removeAllObjects];
    if([[NSUserDefaults standardUserDefaults] valueForKey:USERINFOKEY]){
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERINFOKEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (BOOL)isLogin{
    UserInfo *user = USERINFO;
    if(user.token && user.token.length > 0){
        return YES;
    }else{
        return NO;
    }
}

- (void)tokenloginOut{
    if(![[ClassManager shareInstance].currentViewController isKindOfClass:[LoginViewController class]]){
        [[UserInfoManager sharedInstance] delectUserInfo];
        LoginViewController *vc = [[LoginViewController alloc] init];
        UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
        KEYWINDOW.rootViewController = navVc;

    }
}

- (void)loginOut{
    if(USERINFO.token.length < 1){
        
        [[KRAlartManager shareInstance] showTipsAlertWithTitle:@"登录" decTitle:@"请登录后再尝试" cancleBtnText:@"取消" andOkBtnText:@"去登录" sucessBlock:^(BOOL isSucess) {
            if(isSucess){
                [[UserInfoManager sharedInstance] delectUserInfo];
                LoginViewController *vc = [[LoginViewController alloc] init];
                UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
                KEYWINDOW.rootViewController = navVc;
            }
        }];
    }else{
        if(![[ClassManager shareInstance].currentViewController isKindOfClass:[LoginViewController class]]){
            [[UserInfoManager sharedInstance] delectUserInfo];
            LoginViewController *vc = [[LoginViewController alloc] init];
            UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
            KEYWINDOW.rootViewController = navVc;
        }
    }
}
- (NSMutableArray *)deviceInfoList{
    if(!_deviceInfoList){
        _deviceInfoList = [NSMutableArray array];
    }
    return _deviceInfoList;
}

@end
